<!--
* @Description: 寝室专区商品审核
* @Author: liyujie
* @Date: 2021/03/04
* @Last Modified by: liyujie
* @Last Modified time: 2021/03/04
-->
<template>
    <div class="wrapper">
        <div class="content-wrapper">
            <!-- header -->
            <div class="header">
                <div class="header-title">
                    寝室专区
                </div>
            </div>
            <!-- 筛选-->
            <div class="filter-view">
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        商品名称
                    </p>
                    <el-input
                        placeholder="请输入商品名称"
                        v-model="filterData.goods_keyword"
                        suffix-icon="el-icon-search"
                        clearable
                        size="medium"
                        @keydown.enter.native="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        店铺名称
                    </p>
                    <el-input
                        placeholder="请输入店铺名称"
                        v-model="filterData.shop_keyword"
                        suffix-icon="el-icon-search"
                        clearable
                        size="medium"
                        @keydown.enter.native="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">审核状态:</p>
                    <el-select
                        v-model="filterData.verify"
                        filterable
                        placeholder="请选择"
                        size="medium"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in statusList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="getTableData(1)"
                    >
                        查询
                    </el-button>
                </div>
            </div>
            <!--筛选-->

            <!-- table -->
            <el-table
                :data="tableData"
                class="data-table"
                height="100%"
                ref="table"
                style="width: 100%;"
                v-loading="tableLoading"
                @select="handleSelect"
                @select-all="handleSelectAllTop"
                @selection-change="handleSelectionChange"
            >
                <el-table-column
                    type="selection"
                    width="60"
                    align="center"
                    fixed="left"
                />
                <el-table-column
                    type="index"
                    width="80"
                    label="序号"
                    align="center"
                />
                <el-table-column
                    prop="goods_cover"
                    label="商品信息"
                    :formatter="emptyFormatter"
                    align="center"
                >
                    <template slot-scope="scope">
                        <img
                            class="goods-cover"
                            :src="scope.row.goods_cover"
                            @click="fnShowPreview(scope.row.goods_cover)"
                        >
                    </template>
                </el-table-column>
                <el-table-column
                    prop="goods_title"
                    label="商品名称"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                    align="left"
                />
                <el-table-column
                    prop="goods_no"
                    label="商品编号"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                    align="left"
                />
                <el-table-column
                    prop="shop_title"
                    label="店铺名称"
                    show-overflow-tooltip
                    align="left"
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    label="学生价格"
                    prop="goods_price"
                    show-overflow-tooltip
                    align="center"
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    prop="goods_stock"
                    label="剩余库存"
                    show-overflow-tooltip
                    align="center"
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    prop="goods_sale"
                    label="销量"
                    show-overflow-tooltip
                    align="center"
                />
                <el-table-column
                    label="审核状态"
                    show-overflow-tooltip
                    align="center"
                    width="120"
                >
                    <template slot-scope="scope">
                        <div class="status-name">{{ scope.row.verify | statusName }}</div>
                    </template>
                </el-table-column>
                <el-table-column
                    label="提交时间"
                    align="center"
                    width="200"
                >
                    <template slot-scope="scope">
                        {{ scope.row.submit_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm:ss') }}
                    </template>
                </el-table-column>
                <el-table-column
                    label="是否加入寝室专区"
                    show-overflow-tooltip
                    align="center"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        <div v-if="scope.row.join_room_series_cate === 1">是</div>
                        <div v-if="scope.row.join_room_series_cate === 0">否</div>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="goods_view"
                    label="总浏览量"
                    show-overflow-tooltip
                    align="center"
                />
                <el-table-column
                    label="操作"
                    align="left"
                    width="120"
                >
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            size="medium"
                            :disabled="scope.row.verify !== 1"
                            @click="handleVerify(scope.row, 'pass')"
                        >
                            通过
                        </el-button>
                        <el-button
                            type="text"
                            size="medium"
                            :disabled="scope.row.verify === -1"
                            @click="showRejectDialog(scope.row)"
                        >
                            拒绝
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- /table -->

            <!-- /分页 -->
            <div class="pagination-wrapper clearfix">
                <el-checkbox
                    v-model="checkedAll"
                    :indeterminate="checkIndeterminate"
                    @change="handleSelectAll"
                >
                    全选
                </el-checkbox>
                <el-button
                    type="text"
                    :disabled="multipleSelection.length === 0"
                    style="margin-left: 12px"
                    @click="setGroup()"
                >
                    设置商品组
                </el-button>
                <el-pagination
                    layout="total, prev, pager, next, jumper"
                    background
                    :current-page.sync="page.now_page"
                    :total="page.total_count"
                    :page-size="page.page_size"
                    @current-change="getTableData"
                />
            </div>
        </div>
        <!-- 图片预览 -->
        <img-preview ref="preview" />
        <!-- 设置商品组 -->
        <shop-group
            ref="shopGroup"
            @sendData="getData"
        />
        <!-- 驳回弹窗 -->
        <reject-dialog
            ref="rejectDialog"
            :visible.sync="rejectDialog"
            :reason-detail="reasonDetail"
            :maxlength="100"
            @confirm="handleVerify(editRow, 'reject', $event)"
        />
    </div>
</template>

<script>
import {back} from '@/mixin/back';
import imgPreview from '@/components/common/img-preview';
import shopGroup from '@/components/student/shop-group';
import rejectDialog from '@/components/dialog/reject-dialog';

export default {
    name: 'DormGoodsVerify',
    mixins: [back],
    components: {imgPreview, shopGroup,rejectDialog},
    data() {
        return {
            filterData: {
                goods_keyword: '',
                // 店铺信息
                shop_keyword: '',
                verify: 99,
            },
            tableData: [],
            tableLoading: false,
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            statusList: [
                {value: 99,label: '全部'},
                {value: 1,label: '待审核'},
                {value: 2,label: '已通过'},
                {value: -1,label: '未通过'}
            ],
            checkedAll: false,
            checkIndeterminate: false,
            multipleSelection: [],
            editRow: {},
            rejectDialog: false,
            reasonDetail: '',
            authResult: {}
        };
    },
    filters: {
        statusName(val) {
            switch (val) {
                case 1:
                    return '待审核';
                case 2:
                    return '已通过';
                case -1:
                    return '未通过';
                default:
                    return '--';
            }
        },
    },
    computed: {},
    watch: {},
    mounted() {
        this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '商城'}, {title: '寝室专区'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },

    methods: {

        /**
         * @description 获取列表数据
         * @param page
         */
        getTableData(page) {

            this.tableLoading = true;
            let params = {...this.filterData};

            params.page_id = page || this.page.now_page;

            this.$post('/student/shop_tool%5Cgoods_submit_d202103%5Cget_list', params, res => {
                if (res.code === 1) {
                    this.tableData = res.data.list;
                    this.page = res.data.page;
                }
                this.tableLoading = false;
            });
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 路由跳转
         * @param routeName
         * @param query
         */
        handleRoute(routeName, query) {
            this.$router.push({
                name: routeName,
                query
            });
        },

        /**
         * @description 查看图片
         * @param   {String}    images    单图预览
         * @param   {Array}     images     多图预览
         */
        fnShowPreview(images) {
            if(images && Object.prototype.toString.call(images) == '[object String]') {
                this.$refs.preview.show([
                    images
                ]);
            } else if (images && Object.prototype.toString.call(images) == '[object Array]') {
                this.$refs.preview.show(images);
            } else {
                this.$notify({
                    title: '提示',
                    message: '图片类型错误',
                    type: 'warning',
                    duration: 2000
                });
            }
        },

        /**
         * 全选
         */
        handleSelectAll() {
            if(this.checkedAll){
                this.$refs.table.toggleAllSelection();
            }else{
                this.$refs.table.clearSelection();
            }
        },

        /**
         * @description 批量商品组
         */
        setGroup () {
            this.$refs.shopGroup.show();
            this.$refs.shopGroup.getType('add');
        },

        /**
         * 显示拒绝原因弹窗
         */
        showRejectDialog(row) {
            this.editRow = row;
            this.rejectDialog = true;
        },

        /**
         * 获取推荐到商品组数据
         */
        getData (resp) {

            let rejectList = this.multipleSelection.filter(item => {
                return item.verify !== 2;
            });

            if(rejectList.length) {
                this.$message.warning('只有审核通过商品可加入商品组');
                return;
            }

            let goods_nos = this.multipleSelection.map(item => {
                return item.goods_no;
            });

            this.$post('/student/shop_goods%5Cjoin_cate', {
                goods_nos: goods_nos.join(),
                data_nos: resp.data.join()
            }, resp => {
                if (resp.code === 1) {
                    this.$refs.shopGroup.hide();
                    this.checkedAll = false;
                    this.checkIndeterminate = false;
                    this.getTableData();
                    this.$notify({
                        title: '提示',
                        type: 'success',
                        message: '操作成功'
                    });
                } else {
                    this.$notify({
                        title: '提示',
                        type: 'warning',
                        message: resp.msg
                    });
                }
            });
        },

        /**
         * @description 全选逻辑操作
         */
        handleSelect(selection) {
            this.checkedAll = selection.length === this.tableData.length;
            this.checkIndeterminate = selection.length > 0 && selection.length < this.tableData.length;
        },
        handleSelectAllTop(selection) {
            this.checkedAll = selection.length > 0;
            this.checkIndeterminate = selection.length > 0 && selection.length < this.tableData.length;
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },

        /**
         * 审核通过或拒绝
         * @param row
         * @param action pass通过 reject拒绝
         * @param verify_fail_reason
         */
        handleVerify(row, action, verify_fail_reason = '') {
            if(!this.authResult.auth_verify) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }

            this.$confirm(`确认${action === 'pass' ? '通过' : '拒绝'}该商品审核吗?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let params = {
                    id: row.id,
                    verify: action === 'pass' ? 2 : -1,
                    verify_fail_reason
                };

                this.$post('/student/shop_tool%5Cgoods_submit_d202103%5Cverify', params ,resp=>{
                    if (resp.code === 1) {
                        this.getTableData();
                        this.$notify({
                            title: '提示',
                            message: '操作成功',
                            type: 'success',
                            duration: 2000
                        });
                    } else {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            type: 'warning',
                            duration: 2000
                        });
                    }
                });
            });
        }
    }
};
</script>

<style lang="less" scoped>
.wrapper {
    width: 100%;

    .content-wrapper {
        display: flex;
        height: calc(100% - 24px);
        flex-direction: column;
        position: relative;
        margin: 12px;
        padding: 24px;
        background: #fff;
        box-sizing: border-box;

        /*header*/

        .header {
            box-sizing: border-box;
            display: flex;
            align-items: center;
            justify-content: space-between;

            .header-title {
                color: #333333;
                font-size: 20px;
                height: 20px;
                font-weight: 500;
                position: relative;
                margin-left: 12px;
                margin-bottom: 24px;

                &::before {
                    content: '';
                    position: absolute;
                    left: -8px;
                    top: -1px;
                    width: 3px;
                    height: 20px;
                    background: #1890FF;
                }
            }
        }

        /*筛选*/

        .filter-view {
            margin-top: 8px;
            box-sizing: border-box;
            display: flex;
            align-items: center;
            flex-wrap: wrap;

            .header-new-box {
                display: flex;
                align-items: center;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #333333;
                white-space: nowrap;
                margin-right: 24px;
                margin-bottom: 24px;

                &-title {
                    margin-right: 16px;
                    white-space: nowrap;
                }
            }
        }

        .data-table {
            ::-webkit-scrollbar {
                width: 6px;
                height: 6px;
            }
            .goods-cover {
                width: 50px;
                height: 50px;
                margin-right: 10px;
                cursor: pointer;
            }
        }

        /* 分页 */

        .pagination-wrapper {
            padding-top: 10px;

            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }

    /deep/ .el-dialog__header {
        padding-top: 10px;
        box-shadow: 0 1px 0 0 rgba(233, 233, 233, 1);
        display: flex;
        align-items: center;
        background: #FAFAFA;
        border-radius: 4px 4px 0 0;

        .el-dialog__title {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }

        .el-icon-close:before {
            color: rgb(194, 194, 194);
            font-size: 17px;
            font-weight: bold;
        }

        .el-dialog__headerbtn {
            top: 14px;
        }
    }

    /deep/ .el-textarea__inner {
        resize: none;
    }
}
</style>

